# ------------------------------------------------------------------------------------------------
### Generate Table 3: Effects on vote share and turnout by party
# ------------------------------------------------------------------------------------------------

# Load data 
load("../data/results/sdid_results.Rda")

# Filter and prepare data
filtered_results <- filter_sdid_results(
  sdid_results,
  party_filter = c("Law and Justice (PiS)", "Civic Coalition (KO)", "Polish Coalition (KP)", "The Left (Lewica)"),
  outcome_filter = c("Party Turnout", "Vote Share")
) %>%
  mutate(
    party = factor(party, levels = c("Law and Justice (PiS)", "Civic Coalition (KO)", "Polish Coalition (KP)", "The Left (Lewica)")),
    outcome = factor(outcome, levels = c("Party Turnout", "Vote Share")),
    sample = ifelse(sample == "< 50km Sample", "$<$ 50km Sample", sample),
    sample = factor(sample, levels = c("$<$ 50km Sample", "Full Sample"))
  ) %>%
  arrange(outcome, party)

# Generate and save table
latex_table <- create_sectioned_latex_table(
  filtered_results,
  group_var = "party",
  row_var = "sample", 
  section_var = "outcome",
  caption = "Synthetic Difference-in-Differences Estimates for Party-Specific Turnout and Vote Shares",
  label = "tab:sep_parties",
  col_names = c("Party", "Sample", "Estimate (SE)", "N")
)

save_table(latex_table, "../output/tables/table3.txt")
